package com.yanqu.road.dao.impl.activity.stronghold;

import com.alibaba.fastjson.JSON;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.stronghold.StrongholdEliminateDefendPatrons;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class StrongholdEliminatePatronsDaoImpl extends TempDataObjectDao<StrongholdEliminateDefendPatrons> {
    public static String getStrongholdEliminateDefendPatronsSql = "select * from t_u_activity_stronghold_eliminate_patrons where activityId=?";
    public Map<String, Map<Long, List<StrongholdEliminateDefendPatrons>>> getStrongholdEliminateDefendPatrons(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, Map<Long, List<StrongholdEliminateDefendPatrons>>> unionMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getStrongholdEliminateDefendPatronsSql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdEliminateDefendPatrons strongholdEliminateDefendPatrons = parse(rs);
                String unionUid = strongholdEliminateDefendPatrons.getUnionUid();
                Map<Long, List<StrongholdEliminateDefendPatrons>> patronsMap = unionMap.get(unionUid);
                if (patronsMap == null) {
                    patronsMap = new ConcurrentHashMap<>();
                    unionMap.put(unionUid, patronsMap);
                }
                List<StrongholdEliminateDefendPatrons> patronsList = patronsMap.computeIfAbsent(strongholdEliminateDefendPatrons.getUserId(), k -> new ArrayList<>());
                patronsList.add(strongholdEliminateDefendPatrons);

            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return unionMap;
    }

    public static String updateStrongholdEliminateDefendPatronsSql = "update t_u_activity_stronghold_eliminate_patrons set userBaseInfo=?,earnSpeed=?,totalBlood=?,patrons=? where activityId=? and userId=? and unionUid=? and patronsSite = ?";
    @Override
    protected boolean updateData(StrongholdEliminateDefendPatrons strongholdEliminateDefendPatrons) {
        strongholdEliminateDefendPatrons.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdEliminateDefendPatrons.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateDefendPatrons.getEarnSpeed().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateDefendPatrons.getTotalBlood().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdEliminateDefendPatrons.getPatrons())));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdEliminateDefendPatrons.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdEliminateDefendPatrons.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateDefendPatrons.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdEliminateDefendPatrons.getPatronsSite()));
        if (getDb().executeNoneQuery(updateStrongholdEliminateDefendPatronsSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertStrongholdEliminateDefendPatronsSql = "insert into t_u_activity_stronghold_eliminate_patrons(activityId,userId,unionUid,patronsSite,userBaseInfo,earnSpeed,totalBlood,patrons) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(StrongholdEliminateDefendPatrons strongholdEliminateDefendPatrons) {
        strongholdEliminateDefendPatrons.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, strongholdEliminateDefendPatrons.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, strongholdEliminateDefendPatrons.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateDefendPatrons.getUnionUid()));
        para.put(i++, new DbParameter(Types.INTEGER, strongholdEliminateDefendPatrons.getPatronsSite()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdEliminateDefendPatrons.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateDefendPatrons.getEarnSpeed().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, strongholdEliminateDefendPatrons.getTotalBlood().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(strongholdEliminateDefendPatrons.getPatrons())));
        if (getDb().executeNoneQuery(insertStrongholdEliminateDefendPatronsSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected StrongholdEliminateDefendPatrons parseData(ResultSet rs) throws SQLException {StrongholdEliminateDefendPatrons strongholdEliminateDefendPatrons = new StrongholdEliminateDefendPatrons();
        strongholdEliminateDefendPatrons.setActivityId(rs.getInt("activityId"));
        strongholdEliminateDefendPatrons.setUserId(rs.getLong("userId"));
        strongholdEliminateDefendPatrons.setUnionUid(rs.getString("unionUid"));
        strongholdEliminateDefendPatrons.setPatronsSite(rs.getInt("patronsSite"));
        strongholdEliminateDefendPatrons.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        strongholdEliminateDefendPatrons.setEarnSpeed(new BigInteger(rs.getString("earnSpeed")));
        strongholdEliminateDefendPatrons.setTotalBlood(new BigInteger(rs.getString("totalBlood")));
        strongholdEliminateDefendPatrons.setPatrons(JSON.parseObject(rs.getString("patrons"), UserPatrons.class));

        return strongholdEliminateDefendPatrons;
    }
}
